/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menus;

import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.MediaComponent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import util.Menu;

/**
 *
 * @author nialldeasy
 */
public class Camera extends Menu {
    private MediaComponent mc = null;
    private Player player = null;
    private VideoControl vc = null;


    protected void execute(Form f) {

         try {
            player = Manager.createPlayer("capture://video");
            player.realize();
            mc = new MediaComponent(player);
            f.addComponent(mc);
            vc = (VideoControl) player.getControl("VideoControl");
            vc.setDisplayFullScreen(true);

        } catch (Exception e) {
        }
          try {
            if(player!=null){
            player.start();
            }

        } catch (MediaException ex) {
            ex.printStackTrace();
        }

    }
public void take_pic() throws MediaException{
try {

            String photos = "fileconn.dir.photos";
            String path = System.getProperty(photos);

            String url = path +"hello.jpg";
            String string = "hello";
            byte data[] = string.getBytes();
            FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
            if (!fconn.exists()) {
                fconn.create();
            }
            OutputStream ops = fconn.openOutputStream();
            ops.write(data);
            ops.close();
            fconn.close();
        }
        catch (IOException ioe) {
            System.out.println("IOException: "+ioe.getMessage());
        }
        catch (SecurityException se) {
            System.out.println("Security exception:" + se.getMessage());
        }
}
    public void recycleBin() {
         try {
            if(player!=null){
            player.stop();
            }
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        vc = null;
        player = null;
        mc = null;
    }
      public void Key_CENTRE(Form f) {
        try {
            take_pic();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        
    }
    public void update() {
      
       // f.setBgImage(null);

       // System.out.println("updating Camera");
        f.repaint();
    }

}
